package com.business.java2cache.core;

import com.business.java2cache.channel.CacheKit;
import com.business.java2cache.channel.EhcacheKit;
import com.business.java2cache.channel.MemcacheKit;
import com.business.java2cache.channel.RedisCacheKit;
import com.business.java2cache.util.CacheException;
import com.business.java2cache.util.ProReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Properties;

/**
 * 缓存入口
 */
public class Java2Cache {

    private final static Logger log = LoggerFactory.getLogger(Java2Cache.class);

    private final static CacheKit channel;
    //读取配置文件
    public static ProReader java2CacheConfig = ProReader.getInstance(CacheConstans.CONFIG_FILE);

    private final static Properties config;

    static {
        try {
            config = java2CacheConfig.fillProperties();
            //获取对外提供的缓存实现类，先从二级获取，二级未设置(none)时，取1级
            String cache_broadcast = CacheConstans.cache_broadcast;

            if (CacheConstans.redis.equalsIgnoreCase(cache_broadcast))
                channel = RedisCacheKit.getInstance();
            else if (CacheConstans.memcache.equalsIgnoreCase(cache_broadcast))
                channel = MemcacheKit.getInstance();
            else if (CacheConstans.ehcache.equalsIgnoreCase(cache_broadcast))
                channel = EhcacheKit.getInstance();
            else
                throw new CacheException("Cache Channel not defined. name = " + cache_broadcast);

        } catch (Exception e) {
            throw new CacheException("Unabled to load java2cache configuration " + CacheConstans.CONFIG_FILE, e);
        }
    }

    public static CacheKit getInstance() {
        return channel;
    }

    public static Properties getConfig() {
        return config;
    }

}
